#pragma once


#include <string>
#include <sstream>
#include <iostream>

#include <jsoncpp/json/json.h>


#include "MyPthread/Log.hpp"

using namespace LogModule;

const std::string Sep = "\r\n";
// 协议格式：
// length\r\n{json}\r\n

// 编码
bool Encode(std::string& message)
{
    // 封装成协议格式
    std::string package = std::to_string(message.length()) + Sep + message+Sep;
    message = package;
    return true;
}

// 解码
bool Decode(std::string &package, std::string *content)
{
    // 解析协议格式
    std::string::size_type pos = package.find(Sep);
    if (pos == std::string::npos)
    {
        return false;
    }

    std::string length_str = package.substr(0, pos);
    int length = std::stoi(length_str);
    int full_length = length_str.length() + Sep.length() + length + Sep.length();

    // 数据包不完整
    if (package.length() < full_length)
    {
        return false;
    }

    // 截取完整数据包
    *content = package.substr(length_str.length() + Sep.length(), length);
    package = package.erase(0, full_length);

    return true;
}




class Request
{
public:
    Request(int x=0, int y=0, char oper='+') : _x(x), _y(y), _oper(oper) {}

    bool Serialize(std::string &out_string)
    {
        // 将结构化数据放入json对象中
        Json::Value root;
        root["x"] = _x;
        root["y"] = _y;
        root["oper"] = _oper;

        Json::StreamWriterBuilder builder;
        std::unique_ptr<Json::StreamWriter> writer(builder.newStreamWriter());


        std::stringstream ss;
        writer->write(root, &ss);
        out_string = ss.str();
        return true;
    }
    bool Deserialize(const std::string& in_string)
    {
        Json::Value root;
        Json::Reader reader;
        bool parsingSuccessful = reader.parse(in_string, root);
        if (!parsingSuccessful)
        {
            std::cout << "parse error" << std::endl;
            return false;
        }

        _x = root["x"].asInt();
        _y = root["y"].asInt();
        _oper = root["oper"].asInt();

        return true;
    }
    void Print() const
    {
        std::cout << "x: " << _x << " y: " << _y << " oper: " << _oper << std::endl;
    }
    int GetX() const { return _x; }
    int GetY() const { return _y; }
    char GetOper() const { return _oper; }

private:
    int _x;
    int _y;
    char _oper;
};


class Response
{
public:
    Response(int res=0, int code=0) : _res(res), _code(code) {}
    bool Serialize(std::string &out_string)
    {
        // 将结构化数据放入json对象中
        Json::Value root;
        root["res"] = _res;
        root["code"] = _code;

        Json::StreamWriterBuilder builder;
        std::unique_ptr<Json::StreamWriter> writer(builder.newStreamWriter());


        std::stringstream ss;
        writer->write(root, &ss);
        out_string = ss.str();
        return true;
    }
    bool Deserialize(const std::string& in_string)
    {
        Json::Value root;
        Json::Reader reader;
        bool parsingSuccessful = reader.parse(in_string, root);
        if (!parsingSuccessful)
        {
            LOG(LogLevel::ERROR) <<"parse error";
            return false;
        }

        _res = root["res"].asInt();
        _code = root["code"].asInt();

        return true;
    }
    void Print() const  
    {
        std::cout << "res: " << _res << " code: " << _code << std::endl;
    }
    int GetRes() const { return _res; }
    int GetCode() const { return _code; }
    void SetRes(int res) { _res = res; }
    void SetCode(int code) { _code = code; }
private:
    int _res;
    int _code;   // 出错码，0,1,2,3,4
};